//
//  UMStoreManager.m
//  YRSideViewController
//
//  Created by xiabin on 14/12/29.
//  Copyright (c) 2014年 YueRuo. All rights reserved.


#import "UMNetworkManager.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "UMServer.h"
#import "HardwareService.h"



static UMNetworkManager *sharedManager;

@implementation UMNetworkManager

+(UMNetworkManager*)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager=[[UMNetworkManager alloc]init];
        [sharedManager initManager];
    });
    return sharedManager;
}

//初始化
-(void) initManager
{
    jobcookie = 1;
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    curVersion = [infoDic objectForKey:@"CFBundleVersion"];
    uid = [[HardwareService sharedInstance] userid];
}

- (NSDictionary*) JsonParse:(NSString*) JsonData
{
    if (JsonData == nil)
        return nil;
    NSError *error;
    NSDictionary *jsonres = [NSJSONSerialization JSONObjectWithData:[JsonData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
    //NSDictionary *jsonres = [NSJSONSerialization JSONObjectWithData:[JsonData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
    return jsonres;
}

-(int) ThirdPartyLogin:(NSString*)openId Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + GetCityInfoJob*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_3rdLogin)];
    NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                         openId, @"openid", nil];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error: nil];
    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
    
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:tempJsonData];
    [request buildRequestHeaders];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetPwdSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetPwdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) GetPwd:(NSString*)phoneNumber Callback: (id<UMAsyncJobDelegate>)JobDelegate
{
    int jobid = jobcookie + GetDyncPwdJob*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetPwd(phoneNumber))];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetPwdSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetPwdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) GetCityInfo:(NSString*)cityName Callback: (id<UMAsyncJobDelegate>)JobDelegate;
{
    int jobid = jobcookie + GetCityInfoJob*1000 + rand();
    jobcookie += 1;
    
    
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetCityInfo([cityName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]))];

    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) GetPostsByCategoryId:(int)CategoryId WithPageNum:(int)PageNum LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate
{
    int jobid = jobcookie + JobPostsByCategoryId*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetPostsByCategory(CategoryId, uid, ts, PageNum))];

    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetPostsByCategoryIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetPostsByCategoryIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetCommentsByPostId:(int)PostId WithPageNum:(int)PageNum LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetCommentsByPostId*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetCommentsByID(PostId,ts,PageNum))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetCommentsByProductId:(int)ProductId WithPageIdx:(int)PageIdx LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetCommentsByProductId*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetProductComments(ProductId,ts,PageIdx))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

-(int) GetMyCommentsByUIdWithPageIdx:(int)PageIdx LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetMyComments*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetMyCommentsByID(uid,ts,PageIdx))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetMyLikesByUIdWithPageIdx:(int)PageIdx LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetMyLikes*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetMyLikesByID(uid,ts,PageIdx))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetUserFollowsUID:(int)UserID andPage:(int)PageIdx LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetFollowers*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetUIDFollowing(UserID,ts,PageIdx))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

-(int) GetUserCard:(int)targetUid LastTs:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetUserCard*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetUserCard(targetUid, uid, ts))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

-(int) GetSelfInfoLastTs:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    int jobid = jobcookie + JobGetUserCard*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetSelfInfo(uid, ts))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetUserFansUID:(int)UserID andPage:(int)PageIdx LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetFollowers*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetUIDFans(UserID,ts,PageIdx))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetUserPosts:(int)targetUid Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetFollowers*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetUserPosts(targetUid))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

-(int) GetOrderInStatus:(int)status Page:(int)PageIdx LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetFollowers*1000 + rand();
    jobcookie += 1;
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetOrders(uid, ts, PageIdx, status))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetPostByPostId:(int)PostId LastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    
    int jobid = jobcookie + JobGetPostByPostId*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetPostByID(uid, PostId, ts))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) LoginWithNameAndPwd:(NSMutableData*)userinfo Callback: (id<UMAsyncJobDelegate>)JobDelegate
{
    int jobid = jobcookie + Login*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Login)];
    
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:userinfo];
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UpdateInfoSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UpdateInfoFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    
    return jobid;
}

-(int) RegUserNick:(NSString*)userNick Gender:(int)genderId HeadData:(NSData*)imagedata Callback: (id<UMAsyncJobDelegate>)JobDelegate
{
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    int jobid = jobcookie + RegUser*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Reginfo)];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    [request addPostValue:userNick forKey:@"nickname"];
    [request addPostValue:[NSNumber numberWithInt:genderId] forKey:@"gender"];
    [request addPostValue:[NSNumber numberWithInt:uid] forKey:@"uid"];
    //[request addData:imagedata forKey:@"avatarFile"];
    //[request addData:imagedata withFileName:@"file" andContentType:@"image/jpeg" forKey:@"avatarFile"];
    [request addData:imagedata withFileName:@"avatarFile.jpg" andContentType:@"image/jpeg" forKey:@"avatarFile"];
    //[request setData:imagedata withFileName:@"avatarFile" andContentType:@"image/jpeg" forKey:@"avatarFile"];
    [request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    
    return jobid;

}

-(int) PublishComment:(int)PostId Content:(NSString*) content replyUid:(int) toUid replyComment:(int) toCommentID ImageDatas:(NSData*)imagedata Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    {
        if (uid == 0) {
            uid = [[HardwareService sharedInstance] userid];
        }
        int jobid = jobcookie + JobPublishComment*1000 + rand();
        jobcookie += 1;
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_PubComment(PostId))];
        
        [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
        [request addPostValue:content forKey:@"content"];
        [request addPostValue:[NSNumber numberWithInt:toUid] forKey:@"replyUid"];
        [request addPostValue:[NSNumber numberWithInt:toCommentID] forKey:@"parentId"];
        [request addPostValue:[NSNumber numberWithInt:uid] forKey:@"uid"];
        
        if (imagedata != nil) {
            [request addData:imagedata withFileName:@"commentimage.jpg" andContentType:@"image/jpeg" forKey:@"file"];
        }
        //[request setData:imagedata withFileName:@"avatarFile" andContentType:@"image/jpeg" forKey:@"avatarFile"];
        [request buildRequestHeaders];
        NSLog(@"header: %@", request.requestHeaders);
        
        [request setTimeOutSeconds:5000];
        
        __weak ASIFormDataRequest* blockRequest = request;
        [request setCompletionBlock:^{
            [MobClick event:@"UploadImageSuccess"];
            if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
                [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
            }
        }];
        [request setFailedBlock:^{
            [MobClick event:@"UploadImageFailed"];
            if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
                [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
            }
        }];
        
        [request startAsynchronous];
        
        return jobid;
    }
}

-(int) PublishPost:(NSString*)title Category:(int)CategoryId Content:(NSString*) content ImageDatas:(NSArray*)imagedata Callback: (id<UMAsyncJobDelegate>) JobDelegate
{
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
        uid = 100004;
    }
    int jobid = jobcookie + JobPublishPost*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_PubPost)];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    [request addPostValue:title forKey:@"title"];
    [request addPostValue:content forKey:@"content"];
    [request addPostValue:[NSNumber numberWithInt:CategoryId] forKey:@"categoryId"];
    [request addPostValue:[NSNumber numberWithInt:uid] forKey:@"uid"];
    
    for (int i=0; i<imagedata.count;i++) {
        NSString* filename = [NSString stringWithFormat:@"file%d.jpg",i+1];
        NSString* key = [NSString stringWithFormat:@"files[%d]",i];
        [request addData:imagedata[i] withFileName:filename andContentType:@"image/jpeg" forKey:key];
    }
    //[request setData:imagedata withFileName:@"avatarFile" andContentType:@"image/jpeg" forKey:@"avatarFile"];
    [request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    
    return jobid;
}


-(int) DoFavorPost:(int)postID Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    int jobid = jobcookie + JobDoFav*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_FavorPost(postID, uid))];
    [request setRequestMethod:@"PUT"];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) ReportPosts:(int)postId Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobDoFav*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Report_Post(uid, postId))];
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) DoFollowUser:(int)userId Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobFollowUser*1000 + rand();
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_UnFollowUser(uid, userId))];
    //[request addRequestHeader:@"Content-Type" value:@"multipart/m-data"];
    //[request addPostValue:[NSNumber numberWithInt:userId] forKey:@"followingUid"];
    //[request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    [request setTimeOutSeconds:5000];
    __weak ASIFormDataRequest* blockRequest = request;

    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) DoUnFollowUser:(int)userId Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobFollowUser*1000 + rand();
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_UnFollowUser(uid, userId))];
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    [request setRequestMethod:@"DELETE"];
    //[request addPostValue:[NSNumber numberWithInt:userId] forKey:@"followingUid"];
    //[request buildRequestHeaders];
    //[request setTimeOutSeconds:5000];
    __weak ASIFormDataRequest* blockRequest = request;
    
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetPostsCategory:(int) timeStamp Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + rand();
    jobcookie += 1;
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_PostsCategory(timeStamp))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

/*-(int) AddShoppingCart:(int)productId andQuantity:(int) num SetCallback:(id<UMAsyncJobDelegate>)JobDelegate {
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    int jobid = jobcookie + JobAddShoppingCark*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_AddShoppingCart)];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];

    [request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    [request addPostValue:[NSNumber numberWithInt:productId] forKey:@"productId"];
    [request addPostValue:[NSNumber numberWithInt:uid] forKey:@"uid"];
    [request addPostValue:[NSNumber numberWithInt:uid] forKey:@"quantity"];
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    
    return jobid;
}*/

-(int) GetProductHomeSingleProductCallback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetProductSingle*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetProductHomeSingle)];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) GetProductHomeCallback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetProduct*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetProduct)];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;

}

-(int) GetProductDetail:(int)ProductID withTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetProductDetail*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_ProductDetail(ProductID, ts))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
}


-(int) GetTopicSellsProducts:(int)TopicID withTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetTopicCellsProduct*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetTopicProdcuts(TopicID, ts))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) GetTopicHomeLastTS:(int)ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetTopic*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetTopic(ts))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
    
}

-(int) GetIMLoginTokenCallback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetIMLoginToken*1000 + rand();
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetRCLoginToken(uid))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
    
}

-(int) GetIMUsersInfo:(NSArray*)users Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetIMLoginToken*1000 + rand();
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    jobcookie += 1;
    
    NSMutableString* userStr = [NSMutableString stringWithFormat:@"%@", users[0]];
    for (int i=1; i<users.count; i++) {
        [userStr appendString:[NSString stringWithFormat:@",%@", users[i]]];
    }

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetUsersInfoBatch(userStr))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) GetIMGroupsCallback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetIMGroups*1000 + rand();
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetUsersGroup(uid))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) GetIMChatrooms:(int) ts Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetIMChatRoom*1000 + rand();
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_GetChatroom(uid, ts))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) SaveAddressInfo:(NSMutableData*)AddressInfo Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobSaveAddress*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_SaveAddress)];
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:AddressInfo];
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UpdateInfoSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UpdateInfoFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) SubmitOrder:(NSMutableData*)OrderInfo Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobSubmitOrder*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_ApplyOrder)];
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:OrderInfo];
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UpdateInfoSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UpdateInfoFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request startAsynchronous];
    return jobid;

}

-(int) UpdateHead:(NSData*)imagedata Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobUpdateHead*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_UpdateHead(uid))];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    [request addData:imagedata withFileName:@"head.jpg" andContentType:@"image/jpeg" forKey:@"avatarFile"];
    [request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    
    return jobid;
}

-(int) GetSettingCallback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetSelfInfo*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Setting(uid))];
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    [request startAsynchronous];
    return jobid;
}

-(int) ReviewProduct:(NSString*) content ProductId:(int) pid Scroll:(double) sc Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobReviewProduct*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_ReViewProduct(pid))];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    
    NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                         content, @"content",
                         [NSNumber numberWithInt:uid], @"uid",
                         [NSNumber numberWithDouble:sc], @"score", nil];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error: nil];
    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
    
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:tempJsonData];
    [request buildRequestHeaders];
    
    NSLog(@"header: %@", request.requestHeaders);
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

-(int) UpdateSetting:(NSString*) type Value:(int) v Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobUpdateSettting*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Setting(uid))];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    
    NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                         type, @"settingCode",
                         [NSNumber numberWithInt:v], @"settingValue", nil];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error: nil];
    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
    
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:tempJsonData];
    [request buildRequestHeaders];
    
    NSLog(@"header: %@", request.requestHeaders);
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) ConfirmOrder:(int) orderId Callback:(id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobConfirmOrder*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Confirm(orderId))];
    [request setTimeOutSeconds:5000];
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) BindBDPush:(NSString*)channelId Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    
    if (uid == 0) {
        uid = [[HardwareService sharedInstance] userid];
    }
    if (uid == 0) {
        return 0;
    }
    int jobid = jobcookie + JobBindBDPush*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Bind_BDPush_Post)];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    
    NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                         [NSNumber numberWithInt:uid], @"uid",
                         [NSNumber numberWithInt:2], @"deviceType",
                         channelId, @"channelId", nil];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error: nil];
    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
    
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:tempJsonData];
    [request buildRequestHeaders];
    
    NSLog(@"header: %@", request.requestHeaders);
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) UpdateUserInfo:(NSDictionary*)infoData Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobUpdateField*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Reginfo)];
    
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
    for (NSString* curkey in [infoData allKeys]) {
        [request addPostValue:infoData[curkey] forKey:curkey];
    }
    //[request addData:imagedata withFileName:@"avatarFile.jpg" andContentType:@"image/jpeg" forKey:@"avatarFile"];
    //[request setData:imagedata withFileName:@"avatarFile" andContentType:@"image/jpeg" forKey:@"avatarFile"];
    [request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) FollowProduct:(int)productID Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobFollowProduct*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Prodcut_FollowOP)];
    
    NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                         [NSNumber numberWithInt:uid], @"uid",
                         [NSNumber numberWithInt:productID], @"productId", nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error: nil];
    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
    
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:tempJsonData];
    
    [request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

-(int) UnFollowProducts:(NSArray*)productsIDArr Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobUnFollowProduct*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Prodcut_FollowOP)];

    NSMutableString* userStr = [NSMutableString stringWithFormat:@"%@", productsIDArr[0]];
    for (int i=1; i<productsIDArr.count; i++) {
        [userStr appendString:[NSString stringWithFormat:@",%@", productsIDArr[i]]];
    }
    
    NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                         [NSNumber numberWithInt:uid], @"uid",
                         userStr, @"productIds", nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error: nil];
    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:tempJsonData];
    [request setRequestMethod:@"PUT"];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

-(int) UpdateShoppingCartItemsStatus:(NSMutableData*)productInfo SetCallback:(id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobFollowProduct*1000 + rand();
    jobcookie += 1;
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UMServerBaseURL(_URL_PARAM_AddShoppingCart)];
    
    [request setPostFormat:ASIMultipartFormDataPostFormat];
    request.stringEncoding = NSUTF8StringEncoding;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"charset" value:@"UTF-8"];
    [request setPostBody:productInfo];
    
    [request buildRequestHeaders];
    NSLog(@"header: %@", request.requestHeaders);
    [request setTimeOutSeconds:5000];
    
    __weak ASIFormDataRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"UploadImageSuccess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"UploadImageFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    
    [request startAsynchronous];
    return jobid;
}

-(int) GetFollowProduct:(int)pgIndex pgSize:(int) pgSize Callback: (id<UMAsyncJobDelegate>)JobDelegate {
    int jobid = jobcookie + JobGetFollowProducts*1000 + rand();
    jobcookie += 1;
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:UMServerBaseURL(_URL_PARAM_Prodcut_GetFollows(uid, pgIndex, pgSize, 0))];
    
    __weak ASIHTTPRequest* blockRequest = request;
    [request setCompletionBlock:^{
        [MobClick event:@"GetCommentsByPostIdSucess"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:SUCCESS JobCookie:jobid And:[self JsonParse:blockRequest.responseString]];
        }
    }];
    [request setFailedBlock:^{
        [MobClick event:@"GetCommentsByPostIdFailed"];
        if ([JobDelegate respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
            [JobDelegate UMAsyncJobDone:FAILED JobCookie:jobid And:nil];
        }
    }];
    
    [request startAsynchronous];
    return jobid;

}

@end
